﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.Controller;

namespace WP09S03_OfficeTracking
{
    public partial class FrmCategory : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        public FrmCategory()
        {
            InitializeComponent(); 
        }


        private void FrmCategory_Load(object sender, EventArgs e)
        {
        
            try
            {
                this.categoryTableAdapter.Fill(this.dSItem.Category);
                updateTotal();
            }
            catch (Exception ex) {
                CatchError(ex);
            }
           

            
             
        }



        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                dgvCategory.EndEdit();
                this.categoryTableAdapter.Update(this.dSItem.Category);
            }
            catch (Exception ex)
            {
                CatchError(ex);
            }

            try {
                DiagAddCategory d = new DiagAddCategory();
                d.ShowDialog();
                this.dSItem.Clear();
                this.categoryTableAdapter.Fill(this.dSItem.Category);
                updateTotal();
              
            }
            catch (Exception ex) {
                CatchError(ex);
            }
          
        }

       
        private void btnSearch_Click(object sender, EventArgs e)
        {
            int con ;
             if (rbAll.Checked ){
             con=-1;
             }
             else if (rbYes.Checked ){
             con=1;
             }
             else {
             con=0;
             }

             try
             {
                 this.dSItem.Clear();
                 this.categoryTableAdapter.FillBySearch(this.dSItem.Category, txtCategoryID.Text, txtCategoryName.Text, con);
                 updateTotal();
             }
             catch (Exception ex) {

                 CatchError(ex);
             }

           //  dgvCategory.DataBindings();

        }

        private void updateTotal() {
            SetMessage("Total " + this.dSItem.Category.Count.ToString() + " Records!");
        
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try {
                dgvCategory.EndEdit();
                this.categoryTableAdapter.Update(this.dSItem.Category);
                SetMessage("Update Successfully!");
            }
            catch (Exception ex) {
                CatchError(ex);
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            DataGridViewRow row = dgvCategory.CurrentRow;

            if (row == null)
            {
                MessageBox.Show("No Record Seletectd");
                return;   
            }
        

            int con = 0;

            if (row.IsNewRow == false)
            {
                //check items belong to category.

                Controller.ItemController ic = new Controller.ItemController();

                con = ic.CheckCategoryItem(Convert.ToInt32(row.Cells[0].Value));

                 
            }
           

          if (con > 0)
          {
 
              CatchError(new Exception("Cannot delete this one due to category is not empty!"));

          }
          else {
            if (ShowDeleteConfirm() == DialogResult.Yes) {
                dgvCategory.Rows.Remove(row);
                updateTotal();
            }
          }
        }

        private void dgvCategory_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            updateTotal();
            if (e.RowIndex > -1)
            {
                btnDelete.Enabled = true;

            }
            else {
                btnDelete.Enabled = false;
            }
        }

        private void dgvCategory_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

            e.Cancel = true;
            CatchError(new Exception("Invalid Data entered!"));
        }

   
       
    }
}
